<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryRef"
      :inline="true"
      v-show="showSearch"
    >
      <el-form-item label="分组名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入分组名称"
          clearable
          style="width: 200px"
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="能源类型" prop="energyType">
        <el-select
          v-model="queryParams.energyType"
          clearable
          placeholder="请选择能源类型"
        >
          <el-option
            v-for="item in ENERGY_TYPE_OPTIONS"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['device:propertyGroup:add']"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button type="primary" plain icon="Search" @click="toggleSearch">{{
          showSearch ? "隐藏搜索" : "显示搜索"
        }}</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="primary" plain icon="Refresh" @click="getList"
          >刷新</el-button
        >
      </el-col>
    </el-row>

    <el-table
      v-loading="loading"
      :data="tableData"
      style="min-height: 500px; height: 600px"
    >
      <el-table-column prop="index" align="center" label="序号" width="80">
        <template #default="scope">
          {{ scope.$index + 1 }}
        </template>
      </el-table-column>
      <el-table-column
        prop="name"
        align="center"
        label="分组名称"
        width="200"
      />
      <el-table-column
        label="能源类型"
        align="center"
        prop="energyType"
        width="120"
      >
        <template #default="scope">
          <el-tag :type="getEnergyTypeTagType(scope.row.energyType)">
            {{ getEnergyTypeName(scope.row.energyType) }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template #default="scope">
          <el-button
            type="text"
            icon="Edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['device:propertyGroup:edit']"
            >修改</el-button
          >
          <el-button
            type="text"
            icon="Delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['device:propertyGroup:remove']"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <!-- 添加或修改设备属性分组对话框 -->
    <el-dialog
      :title="title"
      v-model="open"
      width="50%"
      append-to-body
      destroy-on-close
    >
      <el-form ref="formRef" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="分组名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入分组名称" />
        </el-form-item>
        <el-form-item label="能源类型" prop="energyType">
          <el-select v-model="form.energyType" placeholder="请选择能源类型">
            <el-option
              v-for="item in ENERGY_TYPE_OPTIONS"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button
            type="primary"
            @click="submitForm"
            v-hasPermi="['device:propertyGroup:save']"
            >确 定</el-button
          >
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>

    <!-- 分页 -->
    <el-pagination
      @current-change="currentChange"
      :current-page="pagination.curPage"
      :page-size="pagination.pageSize"
      layout="total, prev, pager, next"
      :total="pagination.total"
    />
  </div>
</template>

<script setup>
import {
  getPropertyGroupList,
  savePropertyGroup,
  delPropertyGroup,
} from "@/api/deviceManage/propertyGroup";
import { ElMessage, ElMessageBox } from "element-plus";
import { reactive, ref, onMounted, getCurrentInstance } from "vue";
import {
  ENERGY_TYPE_OPTIONS,
  getEnergyTypeName,
  getEnergyTypeTagType,
} from "@/utils/enums";

const { proxy } = getCurrentInstance();
const tableData = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const title = ref("");

const queryParams = reactive({
  name: "",
  energyType: null,
});

const pagination = reactive({
  curPage: 1,
  pageSize: 10,
  total: 0,
});

const form = reactive({
  id: null,
  name: "",
  energyType: null,
});

const rules = reactive({
  name: [{ required: true, message: "分组名称不能为空", trigger: "blur" }],
  energyType: [
    { required: true, message: "能源类型不能为空", trigger: "change" },
  ],
});

/** 查询列表 */
function getList() {
  loading.value = true;
  let params = { ...pagination, ...queryParams };
  getPropertyGroupList(params)
    .then((response) => {
      tableData.value = response.data.list;
      pagination.total = response.data.totalCount;
      loading.value = false;
    })
    .catch((error) => {
      console.error("获取设备属性分组列表失败:", error);
      loading.value = false;
      ElMessage.error("获取设备属性分组列表失败，请稍后重试");
    });
}

/** 取消按钮 */
function cancel() {
  open.value = false;
  reset();
}

/** 表单重置 */
function reset() {
  Object.assign(form, {
    id: null,
    name: "",
    energyType: null,
  });
  proxy.resetForm("formRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  pagination.curPage = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  queryParams.name = "";
  queryParams.energyType = null;
  handleQuery();
}

/** 切换搜索显示/隐藏 */
function toggleSearch() {
  showSearch.value = !showSearch.value;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "新增设备属性分组";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  Object.assign(form, row);
  open.value = true;
  title.value = "修改设备属性分组";
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["formRef"].validate((valid) => {
    if (valid) {
      savePropertyGroup(form).then((response) => {
        proxy.$modal.msgSuccess(form.id ? "修改成功" : "新增成功");
        open.value = false;
        getList();
      });
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  ElMessageBox.confirm(
    '是否确认删除名称为"' + row.name + '"的数据项?',
    "提示",
    {
      confirmButtonText: "确定",
      cancelButtonText: "取消",
      type: "warning",
    }
  )
    .then(() => {
      delPropertyGroup([row.id]).then((res) => {
        getList();
        proxy.$modal.msgSuccess("删除成功");
      });
    })
    .catch(() => {
      ElMessage.info("取消删除");
    });
}

function currentChange(e) {
  pagination.curPage = e;
  getList();
}

// 页面初始化
onMounted(() => {
  getList();
});
</script>

<style lang="scss" scoped>
@import "@/assets/styles/variables.module.scss";

.app-container {
  padding: 10px 20px;
  background: $base-menu-background;
  min-height: 100vh;
}

// 搜索表单样式
.el-form {
  background: #ffffff;
  padding: 10px 20px;
  border-radius: 8px;
  margin-bottom: 10px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);

  .el-button {
    border-radius: 6px;
    font-weight: 500;

    &.el-button--primary {
      background: linear-gradient(135deg, #4fdaca 0%, #32b7cb 100%);
      border: none;
      color: #ffffff;

      &:hover {
        background: linear-gradient(135deg, #3bc4b3 0%, #2aa3b8 100%);
        color: #ffffff;
      }
    }

    &:not(.el-button--primary) {
      background: linear-gradient(135deg, #909399 0%, #73767a 100%);
      border: none;
      color: #ffffff;

      &:hover {
        background: linear-gradient(135deg, #82848a 0%, #6c6e72 100%);
        color: #ffffff;
      }
    }
  }
}

// 按钮组样式
.mb8 {
  margin-bottom: 10px;
  margin-left: 0 !important;
  margin-right: 0 !important;
  background: #ffffff;
  padding: 15px 20px;
  border-radius: 8px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);

  .el-button {
    border-radius: 6px;
    font-weight: 500;

    &.el-button--primary {
      background: linear-gradient(135deg, #4fdaca 0%, #32b7cb 100%);
      border: none;
      color: #ffffff;

      &:hover {
        background: linear-gradient(135deg, #3bc4b3 0%, #2aa3b8 100%);
        color: #ffffff;
      }
    }

    &.el-button--primary.is-plain {
      background: linear-gradient(135deg, #4fdaca 0%, #32b7cb 100%);
      border: none;
      color: #ffffff;

      &:hover {
        background: linear-gradient(135deg, #3bc4b3 0%, #2aa3b8 100%);
        color: #ffffff;
      }
    }
  }
}

// 操作按钮样式
:deep(.el-button--text) {
  color: #4fdaca !important;
  font-weight: 600 !important;
  position: relative !important;
  transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1) !important;
  border-radius: 6px !important;
  padding: 6px 12px !important;

  &:hover {
    color: #32b7cb !important;
    background: linear-gradient(
      135deg,
      rgba(79, 218, 202, 0.1) 0%,
      rgba(50, 183, 203, 0.1) 100%
    ) !important;
    transform: translateY(-1px) !important;
    box-shadow: 0 2px 8px rgba(79, 218, 202, 0.2) !important;
  }

  &:active {
    transform: translateY(0) !important;
    box-shadow: 0 1px 4px rgba(79, 218, 202, 0.3) !important;
  }
}

// 更具体的操作按钮样式选择器
::v-deep(.el-button.el-button--text) {
  color: #4fdaca !important;
  font-weight: 600 !important;
  font-size: 13px !important;
  padding: 8px 16px !important;
  margin: 0 6px !important;
  border-radius: 8px !important;
  border: 1px solid transparent !important;
  transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1) !important;
  position: relative !important;
  overflow: hidden !important;

  &::before {
    content: "";
    position: absolute;
    top: 0;
    left: -100%;
    width: 100%;
    height: 100%;
    background: linear-gradient(135deg, #4fdaca 0%, #32b7cb 100%);
    transition: left 0.3s ease;
    z-index: -1;
  }

  &:hover {
    color: #ffffff !important;
    transform: translateY(-2px) !important;
    box-shadow: 0 4px 12px rgba(79, 218, 202, 0.4) !important;
    border-color: #4fdaca !important;
    background: transparent !important;

    &::before {
      left: 0;
    }
  }

  &:active {
    transform: translateY(0) !important;
    box-shadow: 0 2px 6px rgba(79, 218, 202, 0.3) !important;
  }
}

.el-select {
  width: 220px !important;
}
.el-input {
  width: 100% !important;
}
</style>
